﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Admin.Model;
@using CNKI.TPI.Web.Base
<script>
    //全选
    function dbcheckall(e) {
        if ($(e).attr("checked") == "checked") {
            $(".checkboxdb input[type='checkbox']").each(function () {
                $(this).attr("checked", "checked");
            })
        }
        else {
            $(".checkboxdb input[type='checkbox']").each(function () {
                $(this).removeAttr("checked", "checked");
            })
        }
    }
    $(".checkboxdb input[type='checkbox']").click(function () {
        //循环判断列表中的checkbox，如果全部选中则全选框呈勾选状态
        var m = 0;
        if ($(this).attr("checked") != "checked") {
            $("input.dball").removeAttr("checked", "checked");
        }
        else {
            $(".checkboxdb input[type='checkbox']").each(function () {
                if ($(this).attr("checked") != "checked") {
                    m = 1;
                }
            })
            if (m == 1) {
                $("input.dball").removeAttr("checked", "checked");
            }
            else {
                $("input.dball").attr("checked", "checked");
            }
        }

    })
    //删除 多选
    function deldblist() {
        var ckbs = $(".checkboxdb input[type='checkbox']:checked");
        if (ckbs.size() == 0) {
            messageAlert("请选择要删除的记录！");
            return;
        }
        confirmDialog('确定要删除吗?', function () {
            var checkedstr = [];
            ckbs.each(function () {
                checkedstr.push($(this).val());
            })
            var delurl = "@Html.Raw(Url.Action("DelDB", "Admin_Topic"))";
            var data = { "sysidstr": checkedstr.toString() };
            dealdata(delurl, data);
        })

    }
    //添加专题
    function adddb() {
        $("#sysid").val(0);
        $("#status").val(1);
        $(".talkDiv-title span").html("新建数据源");
        $("#dbname").val("");
        showdb();
    }
    //编辑
    function modifydb(sysid, name, code, staus) {

        $("#sysid").val(sysid);
        $("#status").val(staus);
        $("#dbname").val(name);
        $(".talkDiv-title span").html("编辑数据源");
        showdb();
    }
    //删除
    function deldb(sysid) {
        var delurl = "@Html.Raw(Url.Action("DelDB", "Admin_Topic"))";
        var data = { "sysidstr": sysid };
        confirmDialog('确定要删除吗?', function () {
            dealdata(delurl, data);
        })
    }
    function closedb() {
        $(".talkDivBg").hide();
        $(".dbform").hide();
    }
    function showdb() {
        $(".talkDivBg").show();
        $(".dbform").show();
    }
    //调用controll
    function dealdata(urlstr, datastr) {
        $.ajax({
            type: "POST",
            url: urlstr,
            data: datastr,
            success: function (m) {
                if (m == "True") {
                    messageAlert("操作成功！");
                    ajaxLoad("initload", '@Html.Raw(Url.Action("DBManager", "Admin_Topic"))');
                }
                else {
                    messageAlert("操作失败！");
                }
            },
            error: function (e)
            { }
        })
    }
    function savedb() {
        var dbname = $("#dbname").val();
        if ($.trim(dbname) == "") {
            messageAlert("请输入数据源名称！");
            return;
        }
        else if (!regtext(dbname)) {
            messageAlert("不允许输入特殊字符！");
            return;
        }
        $.ajax({
            type: "GET",
            url: "@Html.Raw(Url.Action("IsExitDB", "Admin_Topic"))?name=" + dbname + "&sysid=" + $("#sysid").val(),
            success: function (obj) {
                if (obj == "True") {
                    messageAlert("该数据源名称已经存在！");
                }
                else {
                    savedata();
                }
            },
            error: function (e) {
            }
        })
    }
    function savedata() {
        var dbname = $("#dbname").val();
        var insertdata = { "Name": dbname, "SysID": $("#sysid").val(), "Status": 1 };
        var saveurl = "@Html.Raw(Url.Action("SaveDB", "Admin_Topic"))";
            var savedata = { "obj": insertdata };
            $.ajax({
                type: "POST",
                url: saveurl,
                data: JSON.stringify(savedata),
                contentType: "application/json",
                success: function (m) {
                    if (m == "True") {
                        messageAlert("操作成功！");
                        closedb();
                        ajaxLoad("initload", '@Html.Raw(Url.Action("DBManager", "Admin_Topic"))');
                    }
                    else {
                        messageAlert("操作失败！");
                    }
                },
                error: function (e)
                { }
            })
        }
        function dealchecked(checkedsub, checkedparent) {
            var m = 0;
            $(checkedsub).each(function () {
                if ($(this).attr("checked") != "checked") {  //遍历判断是否存在没有选中的记录
                    m = 1;
                }
            })
            if (m == 1) {
                $(checkedparent).removeAttr("checked", "checked");
            }
            else {
                $(checkedparent).attr("checked", "checked");
            }
        }
        function setDB(sysid, name, status) {
            ajaxLoad('initload', "@Html.Raw(Url.Action("DBSet", "Admin_Topic"))?sysid=" + sysid + "&name=" + name + "&status=" + status)

        }
</script>

<input type="hidden" id="sysid" value="0" />
<input type="hidden" id="status" value="1" />
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="table table-grey">
    <thead>
        <tr class="thead">
            <th class="tdnum">
                <input type="checkbox" class="dball" value="" name="" onclick="dbcheckall(this)">序号</th>
            <th class=''>数据源名称</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @{if (ViewBag.DBList != null)
          {
              int sortno = ViewBag.PageSize * (ViewBag.PageNow - 1) + 1;
              foreach (SYS_CMS_TOPIC_DBCONFIG item in ViewBag.DBList)
              {
                  string str = "even";
                  if (sortno % 2 == 0)
                  {
                      str = "odd";
                  }
            <tr class='@str'>
                <td class="checkboxdb">
                    <input type="checkbox" value="@item.SYSID">@sortno</td>

                <td><a class="atitle" title="@ColumnAliasHelper.GetContentNoRed(item.Name)">@Html.Raw(ColumnAliasHelper.SubContent(item.Name, 50))</a></td>
                <td class="tc">
                    <a onclick="setDB('@item.SYSID', '@item.Name', '@item.Status')" href="javascript:void(0)">配置</a>
                    <a onclick="modifydb('@item.SYSID','@item.Name','@item.Status')" href="javascript:void(0)">编辑</a>
                    <a onclick="deldb('@item.SYSID')" href="javascript:void(0)">删除</a>
                </td>
            </tr>
                  sortno = sortno + 1;
              }
          }

        }
    </tbody>
</table>

@*//分页*@
<div class="pagenav pagenav-02">
    @*上一页*@
    @if (ViewBag.TotalPage > 0)
    {
        if (ViewBag.PageNow != 1)
        {
            int thispage = ViewBag.PageNow - 1;
        <a href="javascript:void(0);" class="page-prev" onclick="searchPageNew('@thispage')" >上一页</a>
        }
        else
        {
        <a href="javascript:return false;"  onclick="return false" class="page-prev disable">上一页</a>
        }
@*循环初始化页码*@
        foreach (string numpage in ViewBag.PageList)
        {
            if (numpage != "0")
            {
                int m = int.Parse(numpage);
                if (m == ViewBag.PageNow)
                {
        <a href="javascript:void(0);" class="active" onclick="searchPageNew('@numpage')">@numpage</a>
                    
                }
                else
                {
        <a href="javascript:void(0);" onclick="searchPageNew('@numpage')">@numpage</a>
                }

            }
            else
            {
        <i class="iconfont icon-dian2"></i>
            }

        }
@*下一页*@
        if (ViewBag.TotalPage > 0 && ViewBag.PageNow != ViewBag.TotalPage)
        {
            int nextpage = ViewBag.PageNow + 1;
        <a href="javascript:void(0);" class="page-next" onclick="searchPageNew('@nextpage')" >下一页</a>
        }
        else
        {
        <a href="javascript:return false;"  onclick="return false" class="page-prev disable">下一页</a>
        }
    }
    @if (@ViewBag.Count == 0)
    {
        <label>暂无数据！</label>
        
    }
    else
    {
          
        <label>共 </label>
        <label>@ViewBag.Count </label>
        <label>条数据 </label>
    }
</div>
<div class='posr' style="padding-bottom: 20px">

    <a class="btn btn-m btn-submit btn-defa-f" href="javascript:void(0);" onclick="adddb()">添加</a>
    <a class="btn btn-m btn-submit btn-defa-f" href="javascript:void(0);" onclick="deldblist()">删除</a>
 
</div>

<!--talkDiv弹出层(不带图片，宽550)-->
<!--透明背景-->
<div class="talkDivBg" style="display: none;">
    <div class="talkDiv talkDivTr talkW-550 form-h dbform" style='margin-top: -135px; display: none'>
        <div class="talkDiv-title">
            <span>新建数据源</span>
            <a class="icon icon-close" title="关闭" onclick="closedb()" href="javascript:void(0);"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body talkDiv-body1">

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label">数据源名称：</label>
                </div>
                <div class="f-ct">
                    <input type="text" value="" class="f-text span5" id="dbname" maxlength="20">
                    <span class="f-label-re">*</span>
                </div>
            </div>

            <div class="f-gp">
                <div class="f-lb">
                    <label class="f-label"></label>
                </div>
                <div class="f-ct" style="margin-left: 150px">
                    <a href="javascript:void(0);" class="btn btn-m btn-prim-f btn-submit" onclick="savedb()">保存</a>
                    &#12288;<a href="javascript:void(0);" class="btn btn-m btn-submit btn-defa-f" onclick="closedb()">取消</a>
                </div>
            </div>

        </div>
    </div>
</div>
